<?php 
require_once(dirname(__FILE__)."/../../config/config.php"); 
include 'user.php';
if ($_REQUEST['method'] == "getAllChannels"){
		getAllChannels(getUserId());
		//$user_id=getUserId();
}
function getUserId(){
		if (isset ($_REQUEST['mac_address'])) {
			
			$result = showuseridBymacaddress($_REQUEST['mac_address']);
			if($result['message']=="0396"){
				return $result['user_id'];
			}
			echo json_encode($result);
			exit();
		}
		if(isset ($_REQUEST['user_name'])&&isset($_REQUEST['password'])) {
			$result = showuseridByUsernameAndPassword($_REQUEST['user_name'], $_REQUEST['password']);
			if($result['message']=="0395"){
				return $result['user_id'];
			}
			echo json_encode($result);
			exit();
		}
}
function getAllChannels($user_id){
		
		$sqlpackage = "select * from zy_user_service where user_id='".$user_id."' and service_id = 14";
		$rsult = mysql_query($sqlpackage);
		$i=0;
		//获取用户的订购列表.
		while($rowsrs = mysql_fetch_array($rsult)){
			$sql="select * from zy_iptv_package_details where package_id=".$rowsrs['package_id'];
			$rs = mysql_query($sql);
			while ($result=mysql_fetch_array($rs)) {
				$arr[$i]=$result['channel_id'];
				$i++;
			}
        }
        $all_channels_sql="SELECT a.* FROM `zy_tvchannel` AS a ,`zy_tvshow` AS b WHERE a.tv_id =b.tv_id ORDER BY b.sort ASC ,a.sort ASC";
		$all_channels_rs=mysql_query($all_channels_sql);
		$message=array();
		while($row=mysql_fetch_array($all_channels_rs)){	
		$str =array(
			'channelid'=>$row['channel_id'],
			'channellistid'=>$row['tv_id'],
	        'description'=>$row['channel_name'],
	        'url'=>$row['channel_url'],
			'tv_img'=>$row['tv_img'],
		);
		//判断该节目信息是否包含在用户订购的套餐列表里.
	            if(in_array($row['channel_id'],$arr)){
	            	$str["available"]=1;
		   		 	array_push($message,$str);
				}else{
					$str["available"]=0;
		   		 	array_push($message,$str);
			}
	   }
	    echo json_encode($message);	   
}
?>